﻿using System.Globalization;

#if WINDOWS
using System.IO;
using System.Windows.Media;
using Bitmap = System.Windows.Media.Imaging.BitmapImage;
#else
using Avalonia.Media;
using Bitmap = Avalonia.Media.Imaging.Bitmap;


#endif

namespace KinonekoSoftware.Foundation.UI.Media
{
    public static partial class MediaFactory
    {
        /// <summary>
        /// 创建纯色画刷。
        /// </summary>
        /// <param name="key">hex颜色值</param>
        /// <returns>返回纯色画刷</returns>
        public static SolidColorBrush ToSolidColorBrush(BrushKey key)
        {
            return new SolidColorBrush(ToColor(key));
        }

        /// <summary>
        /// 创建纯色画刷。
        /// </summary>
        /// <param name="hexCode">hex颜色值</param>
        /// <returns>返回纯色画刷</returns>
        public static SolidColorBrush ToSolidColorBrush(string hexCode)
        {
            return new SolidColorBrush(ToColor(hexCode));
        }


        /// <summary>
        /// 创建纯色画刷。
        /// </summary>
        /// <param name="color">hex颜色值</param>
        /// <returns>返回纯色画刷</returns>
        public static SolidColorBrush ToSolidColorBrush(Color color)
        {
            return new SolidColorBrush(color);
        }


        #region GradientBrush

        public static RadialGradientBrush CreateRadialGradientBrush(Color[] colors, double[] offset)
        {
            colors ??= Array.Empty<Color>();
            offset ??= Array.Empty<double>();
            var len = Math.Min(colors.Length, offset.Length);


#if WINDOWS
            var stops = new GradientStop[len];

            for (var i = 0; i < len; i++)
            {
                stops[i] = new GradientStop
                {
                    Offset = offset[i],
                    Color = colors[i],
                };
            }

            return new RadialGradientBrush
            {
                SpreadMethod = GradientSpreadMethod.Pad,
                GradientStops = new GradientStopCollection(stops),
            };

#else
            var stops = new GradientStops();

            for (var i = 0; i < len; i++)
            {
                stops.Add(new GradientStop
                {
                    Offset = offset[i],
                    Color  = colors[i],
                });
            }

            return new RadialGradientBrush
            {
                SpreadMethod  = GradientSpreadMethod.Pad,
                GradientStops = stops,
            };
#endif
        }

        public static LinearGradientBrush CreateLinearGradientBrush(double startX, double startY, double endX, double endY, Color[] colors, double[] offset)
        {
            colors ??= Array.Empty<Color>();
            offset ??= Array.Empty<double>();
            var len = Math.Min(colors.Length, offset.Length);

#if WINDOWS
            var start = new Point(startX, startY);
            var end = new Point(endX, endY);
            var stops = new GradientStop[len];

            for (var i = 0; i < len; i++)
            {
                stops[i] = new GradientStop
                {
                    Offset = offset[i],
                    Color = colors[i],
                };
            }
            return new LinearGradientBrush
            {
                StartPoint = start,
                EndPoint = end,
                GradientStops = new GradientStopCollection(stops),
            };
#else
            var start = new RelativePoint(new Point(startX, startY), RelativeUnit.Relative);
            var end   = new RelativePoint(new Point(endX, endY), RelativeUnit.Relative);
            var stops = new GradientStops();

            for (var i = 0; i < len; i++)
            {
                stops.Add(new GradientStop
                {
                    Offset = offset[i],
                    Color  = colors[i],
                });
            }

            return new LinearGradientBrush
            {
                SpreadMethod  = GradientSpreadMethod.Pad,
                StartPoint    = start,
                EndPoint      = end,
                GradientStops = stops,
            };

#endif
        }

        #endregion

        #region BitmapImage

        public static Bitmap ToBitmap(Stream stream)
        {
#if WINDOWS
            var bi = new Bitmap();
            bi.BeginInit();
            bi.StreamSource = stream;
            bi.EndInit();
            if(bi.CanFreeze) bi.Freeze();
            return bi;

#else

            var bi = new Bitmap(stream);
            return bi;
#endif
        }


        public static Bitmap ToBitmap(byte[] buffer)
        {
            var stream = new MemoryStream(buffer);
            ;
#if WINDOWS
            var bi = new Bitmap();
            bi.BeginInit();
            bi.StreamSource = stream;
            bi.EndInit();
            if(bi.CanFreeze) bi.Freeze();
            return bi;

#else

            var bi = new Bitmap(stream);
            return bi;
#endif
        }

        #endregion
    }
}